//
//  CYVideoViewController.m
//  cyydt
//
//  Created by chu on 2019/1/14.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYVideoViewController.h"

#import "CYHomeCommendViewController.h"
#import "CYHomeVideoViewController.h"
#import "CYHomeInfomationViewController.h"
#import "CYHomeChuangYeTongViewController.h"
#import "CYSearchViewController.h"
#import "CYMessageCenterViewController.h"

@interface CYVideoViewController ()<UITextFieldDelegate, SGTopScrollMenuDelegate, UIScrollViewDelegate>
{
    RKNotificationHub *_xiaoxiHub;
    NSNumber *_inform;
    NSNumber *_choose;
    NSNumber *_notice;
}
@property (nonatomic, strong) SGTopScrollMenu *topMenu;

@property (nonatomic, strong) UIScrollView *mainScrollView;
@end

@implementation CYVideoViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.topMenu selectLabel:self.topMenu.allTitleLabel[2]];
    [self showVc:2];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = self.rightBarButtonItem;
    self.navigationItem.titleView = self.titleView;
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 44, kWindowW, kWindowH - Height_NavBar - Height_TabBar - 44);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self setChildViewControllers];
    
    [self.view addSubview:self.topMenu];
    
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    // 1 计算滚动的位置
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.mainScrollView.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    [self.topMenu selectLabel:self.topMenu.allTitleLabel[index]];
}

#pragma mark - SGTopScrollMenuDelegate
- (void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    [self showVc:index];
}

- (void)setChildViewControllers{
    CYHomeCommendViewController *commend = [[CYHomeCommendViewController alloc] init];
    commend.view.frame = CGRectMake(0, 0, kWindowW, self.mainScrollView.frame.size.height);
    [self.mainScrollView addSubview:commend.view];
    [self addChildViewController:commend];
    
    CYHomeInfomationViewController *info = [[CYHomeInfomationViewController alloc] init];
    [self addChildViewController:info];
    
    CYHomeVideoViewController *video = [[CYHomeVideoViewController alloc] init];
    [self addChildViewController:video];
    
    CYHomeChuangYeTongViewController *projevt = [[CYHomeChuangYeTongViewController alloc] init];
    [self addChildViewController:projevt];
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    CYSearchViewController *search = [[CYSearchViewController alloc] init];
    [[AppDelegate sharedAppDelegate] pushViewController:search];
    return NO;
}
#pragma mark - 消息
- (void)message{
    if ([YLUserInfo isLogIn]) {
        CYMessageCenterViewController *message = [[CYMessageCenterViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:message];
    }else{
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
    }
}

- (void)getMessageCount{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"message/count"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"未读消息 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                self->_inform = data[@"inform"];
                self->_choose = data[@"choose"];
                self->_notice = data[@"notice"];
                [self->_xiaoxiHub incrementBy:[self->_inform intValue] + [self->_notice intValue]];
            }else{
                
            }
        }
    }];
}

- (UIView *)titleView{
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW - 70, 44)];
        UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 120, 34)];
        [_titleView addSubview:logo];
        logo.image = [UIImage imageNamed:@"logo"];

        UITextField *_titleTF = [[UITextField alloc] init];
        _titleTF.frame = CGRectMake(130, 7, kWindowW - 70 - 130, 30);
        //        _titleTF.center = CGPointMake(kWindowW / 2, 44 / 2);
        _titleTF.borderStyle = UITextBorderStyleNone;
        _titleTF.backgroundColor = RGBA(241, 241, 241, 1);
        [_titleTF setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
        _titleTF.placeholder = @"搜索";
        _titleTF.layer.cornerRadius = 15;
        _titleTF.textColor = RGBOF(0x434343);
        [_titleTF setFont:[UIFont systemFontOfSize:14]];
        _titleTF.returnKeyType = UIReturnKeySearch;
        UIImageView *userNameLeftViewIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_home"]];
        //图片的显示模式
        userNameLeftViewIV.contentMode= UIViewContentModeCenter;
        //图片的位置和大小
        userNameLeftViewIV.frame= CGRectMake(0,0,30,30);
        //左视图默认是不显示的 设置为始终显示
        _titleTF.leftViewMode= UITextFieldViewModeAlways;
        _titleTF.leftView= userNameLeftViewIV;
        _titleTF.delegate = self;
        [_titleView addSubview:_titleTF];
    }
    return _titleView;
}

- (UIBarButtonItem *)rightBarButtonItem{
    if (!_rightBarButtonItem) {
        _rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(message) image:@"message" highImage:@"message"];
        _xiaoxiHub = [[RKNotificationHub alloc] initWithBarButtonItem:_rightBarButtonItem];
        [_xiaoxiHub scaleCircleSizeBy:0.5];
        [_xiaoxiHub moveCircleByX:10 Y:0];
        [_xiaoxiHub hideCount];
    }
    return _rightBarButtonItem;
}

- (SGTopScrollMenu *)topMenu{
    if (!_topMenu) {
        _topMenu = [SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 0, kWindowW , 44) NormalColor:RGBOF(0x999999) SelColor:RGBOF(0xF43636)];
        _topMenu.topScrollMenuDelegate = self;
        _topMenu.titlesArr = @[@"推荐", @"读资讯", @"看视频", @"创业通"];
    }
    return _topMenu;
}

@end
